<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="患者编号" prop="patientId">
        <el-input
          style="width: 230px"
          v-model="queryParams.patientId"
          placeholder="请输入病人编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指导标题" prop="title">
        <el-input
          style="width: 230px"
          v-model="queryParams.title"
          placeholder="请输入指导标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者来源" prop="subId">
        <el-cascader
          style="width: 230px"
          :props="defaultProps"
          @change="handleSubChange"
          clearable
          :options="deptOptions"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="指导状态" prop="status">
        <el-select
          style="width: 230px"
          v-model="queryParams.status"
          placeholder="请选择指导状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.gonitis_medication_advice"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 230px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gonitis:guidance:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gonitis:guidance:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gonitis:guidance:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gonitis:guidance:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="guidanceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="指导编号" align="center" prop="id" v-if="true" />
      <el-table-column label="患者来源" align="center" prop="subName" />
      <el-table-column
        label="患者编号"
        align="center"
        prop="patientId"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="医生编号" align="center" prop="doctorId" />
      <el-table-column label="医生姓名" align="center" prop="doctorName">
        <template slot-scope="scope">
          <span>{{ scope.row.doctorName ? scope.row.doctorName : "暂无" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="指导标题" align="center" prop="title" />
      <el-table-column label="指导状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.gonitis_medication_advice"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="100" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        width="150"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gonitis:guidance:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gonitis:guidance:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['gonitis:guidance:detail']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改健康指导对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择患者" prop="patientId" v-if="!form.id">
          <el-cascader
            style="width: 230px"
            :props="defaultProps"
            v-model="formSub"
            @change="formSubChange"
            :options="subOptions"
          ></el-cascader>
          <el-select style="width: 230px" v-model="form.patientId" placeholder="请选择">
            <el-option
              v-for="item in patientOptions"
              :key="item.id"
              :label="'[姓名] ' + item.name + '  [电话] ' + item.phone"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="指导标题" prop="title">
          <el-input
            v-model="form.title"
            :maxlength="18"
            show-word-limit
            clearable
            placeholder="请输入指导标题"
          />
        </el-form-item>
        <el-form-item label="饮食建议" prop="dietaryAdvice">
          <el-input
            v-model="form.dietaryAdvice"
            type="textarea"
            :maxlength="300"
            :autosize="{ minRows: 4, maxRows: 6 }"
            show-word-limit
            clearable
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="运动建议" prop="exerciseAdvice">
          <el-input
            v-model="form.exerciseAdvice"
            type="textarea"
            :maxlength="300"
            :autosize="{ minRows: 4, maxRows: 6 }"
            show-word-limit
            clearable
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="指导状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.gonitis_medication_advice"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value + ''"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="指导备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            clearable
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="detailTitle"
      :visible.sync="detailOpen"
      width="40%"
      :before-close="handleClose"
    >
      <div v-loading="detailLoading" class="text-details">
        <el-descriptions class="margin-top" :column="2" direction="vertical" border>
          <el-descriptions-item>
            <template slot="label">指导编号</template>
            {{ detailInfo.id }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">患者来源</template>
            {{ detailInfo.subName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">患者编号</template>
            {{ detailInfo.patientId }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">患者姓名</template>
            {{ detailInfo.patientName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">医生编号</template>
            {{ detailInfo.doctorId }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">医生姓名</template>
            {{ detailInfo.doctorName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">创建时间</template>
            {{ detailInfo.createTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">指导标题</template>
            {{ detailInfo.title }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">饮食建议</template>
            {{ detailInfo.dietaryAdvice }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">运动建议</template>
            {{ detailInfo.exerciseAdvice }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">指导备注</template>
            {{ detailInfo.remark ? detailInfo.remark : "暂无" }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGuidance,
  getGuidance,
  delGuidance,
  addGuidance,
  updateGuidance,
  detailGuidance,
} from "@/api/gonitis/guidance";
import { subTree } from "@/api/gonitis/illSub";
import { listBySubId } from "@/api/gonitis/patient";
export default {
  name: "Guidance",
  dicts: ["gonitis_medication_advice"],
  data() {
    return {
      dateRange: [],
      detailInfo: {},
      detailLoading: false,
      detailTitle: "",
      // 详情可见
      detailOpen: false,
      formSub: undefined,
      // 病情列表
      deptOptions: [],
      // 病情多选框映射
      defaultProps: {
        children: "children",
        label: "label",
        value: "id",
      },
      // 患者集合
      patientOptions: [],
      // 病情集合
      subOptions: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健康指导表格数据
      guidanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientId: undefined,
        subId: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "主键不能为空", trigger: "blur" }],
        title: [{ required: true, message: "指导标题不能为空", trigger: "blur" }],
        patientId: [{ required: true, message: "患者编号不能为空", trigger: "change" }],
        dietaryAdvice: [{ required: true, message: "饮食建议不能为空", trigger: "blur" }],
        exerciseAdvice: [
          { required: true, message: "运动建议不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "指导状态不能为空", trigger: "change" }],
      },
    };
  },
  created() {
    this.init();
  },
  methods: {
    /** 关闭详情对话框 */
    handleClose() {
      this.detailOpen = false;
    },
    /** 指导详情 */
    handleDetail(row) {
      this.detailOpen = true;
      this.detailLoading = true;
      this.detailTitle = "健康指导详情";
      detailGuidance(row.id).then((response) => {
        this.detailInfo = response.data;
        this.detailLoading = false;
      });
    },
    /** 病情分类选择框改变 */
    formSubChange(value) {
      this.form.subId = value[value.length - 1];
      // 发送请求查询对应病情的病人
      listBySubId(this.form.subId).then((response) => {
        this.patientOptions = response.data;
      });
    },
    /** 初始化 */
    init() {
      this.getDeptTree();
      this.getList();
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      subTree().then((response) => {
        this.deptOptions = response.data;
        this.subOptions = response.data;
      });
    },
    /** 病情分类选择框改变 */
    handleSubChange(value) {
      this.queryParams.subId = value[value.length - 1];
    },
    /** 查询健康指导列表 */
    getList() {
      this.loading = true;
      listGuidance(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.guidanceList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        patientId: undefined,
        subId: undefined,
        dietaryAdvice: undefined,
        exerciseAdvice: undefined,
        auditorId: undefined,
        status: undefined,
        createTime: undefined,
        updateTime: undefined,
        remark: undefined,
      };
      this.formSub = [];
      this.patientOptions = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加健康指导";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getGuidance(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.form.status = response.data.status + "";
        this.open = true;
        this.title = "修改健康指导";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGuidance(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            this.form.status = 1;
            addGuidance(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除健康指导编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delGuidance(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "gonitis/guidance/export",
        {
          ...this.queryParams,
        },
        `guidance_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
